import { object_Inf } from "@/utils/typings/index.type";
import { dayjs } from "element-plus";
import i18n from "@/international/i18n";

/*
 * @Descripttion:通用工具方法类
 * @LastEditors: xzh
 * @LastEditTime: 2024-04-21 15:48:01
 */
export default class Common_Methods {
  /**
   * @Author: xzh
   * @Descripttion:获取基础接口地址
   * @Param:
   */
  static getBaseUrl = () => {
    let url = <any>import.meta.env.VITE_BASE_API;
    //作为无界子应用接口地址使用基座地址
    if (!!window.__POWERED_BY_WUJIE__) {
      let { baseURL } = window.$wujie.props || {};
      !!baseURL && (url = baseURL);
    }
    return url;
  };
  /**
   * @Author: xzh
   * @Descripttion: i18n实例
   * @Param:
   */
  // static t = i18n().global.t;
  /**
   * @Author: xzh
   * @Descripttion:创建一个GUID
   * @Param:
   */
  static newGuid(): string {
    let guid = "";
    for (let i = 1; i <= 32; i++) {
      let n = Math.floor(Math.random() * 16.0).toString(16);
      guid += n;
      if (i == 8 || i == 12 || i == 16 || i == 20) guid += "-";
    }
    return guid;
  }

  /**
   * @Author: xzh
   * @Descripttion: 创建一个空GUID
   * @Param:
   * @param {number} number-填充数
   */
  static emptyGuid(number: number = 0) {
    let guid = "";
    for (let i = 1; i <= 32; i++) {
      let n = Math.floor(number > 0 ? number : 0 * 16.0).toString(16);
      guid += n;
      if (i == 8 || i == 12 || i == 16 || i == 20) guid += "-";
    }
    return guid;
  }
  /**
   * @Author: xzh
   * @Descripttion:判断是否为guid格式
   * @Param:
   * @param {string} guid
   */
  static isGuidFormat(guid: string) {
    var reg = new RegExp(
      /^[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}$/
    );
    if (reg.test(guid)) {
      return true;
    }
    return false;
  }
  /**
   * @Author: xzh
   * @Descripttion:数组按照某个字段去重
   * @Param:
   * @param {Array} arr-数据源
   * @param {string} attrName-字段名称
   */
  static uniqueArr(
    arr: Array<object_Inf | string>,
    attrName: string = ""
  ): Array<any> {
    const res = new Map();
    return arr.filter(
      (x) =>
        !res.has(!!attrName ? x[attrName] : x) &&
        res.set(!!attrName ? x[attrName] : x, 1)
    );
  }
  /**
   * @Author: xzh
   * @Descripttion:获取Css变量
   * @Param:
   * @param {string} cssProperty
   * @param {any} dom
   */
  static getCssPropertyValue = (
    cssProperty: string,
    dom: any = document.documentElement
  ) => getComputedStyle(dom).getPropertyValue(cssProperty).trim();

  /**
   * @Author: xzh
   * @Descripttion:生成一个位树字符串
   * @Param:
   * @param {number} length 位数
   */
  static generateRandomStr = (length: number = 4) => {
    // 生成一个4位字符串
    const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW";
    let randomStr = "";
    for (let i = 0; i < length; i++) {
      randomStr += chars[Math.floor(Math.random() * chars.length)];
    }
    return randomStr;
  };
  /**
   * @Author: xzh
   * @Descripttion:调换对象数组位置
   * @Param:
   */
  static exchangeArrIndex = (
    dataArr: Array<object_Inf>,
    oldIndex: number,
    newIndex: number
  ) => {
    //获取拖拽的对象
    let item = dataArr[oldIndex];
    //截取拖拽对象原位置
    dataArr.splice(oldIndex, 1);
    //插入至新位置
    dataArr.splice(newIndex, 0, item);

    return item;
  };

  /**
   * @Author: xzh
   * @Descripttion: 时间范围自定义
   * @Param:
   */
  static getShortcuts = () => {
    let { t } = i18n().global;
    return [
      {
        text: t("common.thisWeek"),
        value: [
          dayjs().startOf("week").add(0, "day").format("YYYY-MM-DD"),
          dayjs().endOf("week").add(0, "day").format("YYYY-MM-DD"),
        ],
      },
      {
        text: t("common.thisMonth"),
        value: [
          dayjs().startOf("month").format("YYYY-MM-DD"),
          dayjs().endOf("month").format("YYYY-MM-DD"),
        ],
      },
      {
        text: t("common.thisYear"),
        value: [
          dayjs().startOf("year").format("YYYY-MM-DD"),
          dayjs().endOf("year").format("YYYY-MM-DD"),
        ],
      },
    ];
  };
  /**
   * @Author: xzh
   * @Descripttion: 获取默认今天
   * @Param:
   */
  static getDefaultToDay = () => {
    //8点之前获取昨天日期
    return !!dayjs().isAfter(dayjs(`${dayjs().format("YYYY-MM-DD")} 08:00:00`))
      ? dayjs()
      : dayjs().add(-1, "day");
  };
  /**
   * @Author: xzh
   * @Descripttion: 格式化千分位
   * @Param:
   * @param {number} num
   */
  static toThousands = (num: number) => {
    let reg = /(?!^)(?=(\d{3})+$)/g;
    return num.toString().replace(reg, ",");
  };
  /**
   * @Author: xzh
   * @Descripttion: 下载文件
   * @Param:
   * @param {object_Inf} res
   */
  static handleFile(res: object_Inf) {
    let fileName = decodeURIComponent(
      res.headers["content-disposition"].split(";")[2].split("=")[1]
    );
    if (fileName.startsWith("UTF-8")) {
      fileName = fileName.split("''")[1];
    }
    const fileType = fileName.slice(fileName.lastIndexOf("."));
    let type = "";
    if (fileType === ".xlsx") {
      type =
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    }
    if (fileType === ".xls") {
      type = "application/vnd.ms-excel";
    }
    let blob = new Blob([res.data], { type });
    let link = document.createElement("a"); // 创建a标签
    link.style.display = "none";
    let objectUrl = URL.createObjectURL(blob);
    link.href = objectUrl;
    link.download = fileName;
    link.click();
    URL.revokeObjectURL(objectUrl);
  }
  /**
   * @Author: xzh
   * @Description: 获取文件路径
   * @Param:
   */
  static getFileUrl = (oid: string = ""): any => {
    const baseUrl = this.getBaseUrl();
    return `${baseUrl}/api/File/GetFile?fileOid=${oid}`;
  };
}
